package org.gpp.proj1.robot.action.step;

import java.util.List;
import java.util.Set;

import org.gpp.proj1.particle.ParticleFactory;
import org.gpp.proj1.robot.Robot;
import org.gpp.proj1.robot.part.EquippedPart;
import org.gpp.proj1.robot.part.emissor.Emissor;

import com.jme.scene.Node;
import com.jmex.effects.ParticleManager;

public class EmissionActionStep implements ActionStep {
	
	private String emissorName;
	private ParticleManager particleManager;
	
	public EmissionActionStep( String emissorName, String particle ) {
		this.emissorName = emissorName;
		this.particleManager = ParticleFactory.getInstance().createParticle(particle);
	}

	public void execute( EquippedPart part, List<Robot> targets ) {
		
		Emissor emissor = part.getEmissor( emissorName );
		Set<String> bones = part.getAssignedBoneGroupNames();
		
		// For each bone on the emissor parent list, attach it to the bone
		// and start the particle emission
		for( String boneName : bones ) {
			Node emissorNode = emissor.getEmissorNode( boneName );
			
			if( emissorNode != null ) {				
				part.getParentRobot().attachEmissor( boneName, emissor );
				
				emissorNode.attachChild( particleManager.getParticles() );
			}
		}
	}
	
	public String getEmissorName() {
		return emissorName;
	}
}